Abstract class

abstract class | concrete class
abstract class always involve abstract method
abstract
int add(int x, int y){
// method body(method which does not has body called abstract method)
}
/* add .
{} . */
above expression same as
abstract int add(int x, int y);
상속만을 위한 추상 클래스(모든 추상 메서드를 구현한 추상 클래스 포함)는 new 예약어로 인스턴스 생성 불가능
Template design patter
Car.java
package template;
public abstract class Car {
public abstract void drive();
public abstract void stop();
public void startCar() {
System.out.println(" .");
}
public void turnOff() {
System.out.println(" .");
}
//template method
final public void run() {
startCar();
drive();
stop();
turnOff();
}
}
AICar.java
package template;
public class AICar extends Car {
@Override
public void drive() {
System.out.println(" .");
System.out.println(" .");
}
@Override
public void stop() {
System.out.println(" .");
}
}
ManualCar.java
package template;
public class ManualCar extends Car{
@Override
public void drive() {
System.out.println(" .");
System.out.println(" .");
}
@Override
public void stop() {
System.out.println(" .");
}
}
CarTest.java
package template;
public class CarTest {
public static void main(String[] args) {
System.out.println("=== ===");
Car myCar=new AICar();
myCar.run();
System.out.println("=== ===");
Car hisCar=new ManualCar();
hisCar.run();
}
}
using template method example;
Player.java
package gameLevel;
public class Player {
private PlayerLevel level;
public Player() {
level=new BeginnerLevel();
level.showLevelMessage();
}
public PlayerLevel getLevel() {
return level;
}
public void upgradeLevel(PlayerLevel level) {
this.level=level;
level.showLevelMessage();
}
public void play(int count) {
level.go(count);
}
}
PlayerLevel.java
package gameLevel;
public abstract class PlayerLevel {
public abstract void run();
public abstract void jump();
public abstract void turn();
public abstract void showLevelMessage();
final public void go(int count) {
run();
for(int i=0;i<count;i++) {
jump();
}
turn();
}
}
BeginnerLevel.java
package gameLevel;
public class BeginnerLevel extends PlayerLevel{
@Override
public void run() {
System.out.println(" .");
}
@Override
public void jump() {
System.out.println("Jump .");
}
@Override
public void turn() {
System.out.println("Turn .");
}
@Override
public void showLevelMessage() {
System.out.println("***** . *****");
}
}
AdvancedLevel.java
package gameLevel;
public class AdvancedLevel extends PlayerLevel{
@Override
public void run() {
System.out.println(" .");
}
@Override
public void jump() {
System.out.println(" Jump.");
}
@Override
public void turn() {
System.out.println("Turn .");
}
@Override
public void showLevelMessage() {
System.out.println("***** . *****");
}
}
SuperLevel.java
package gameLevel;
public class SuperLevel extends PlayerLevel {
@Override
public void run() {
System.out.println(" .");
}
@Override
public void jump() {
System.out.println(" Jump .");
}
@Override
public void turn() {
System.out.println(" .");
}
@Override
public void showLevelMessage() {
System.out.println("***** . *****");
}
}
MainBoard.java
package gameLevel;
public class MainBoard {
public static void main(String[] args) {
Player player=new Player();
player.play(1);
AdvancedLevel aLevel=new AdvancedLevel();
player.upgradeLevel(aLevel);
player.play(2);
SuperLevel SLevel=new SuperLevel();
player.upgradeLevel(SLevel);
player.play(3);
}
}
final
사용위치설명
변수final 변수는 상수를 의미합니다.
메서드final 메서드는 하위 클래스에서 재정의할 수 없습니다.
클래스final 클래스는 상속할 수 없습니다.